// index.js
var request = require("../../utils/request.js")

// 获取应用实例
const app = getApp()

Page({
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    navH: app.globalData.navHeight,
    banerList: [{
      "image": "../../images/Frame.png",
      // "link": "http://di.plus"
    }],
    link: '../index/index',
    type:0
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  //列表跳转
  bindGotoList(e) {
    console.log(e)
    if (e.currentTarget.dataset.page == 'gdjc') {
      this.toastDialog.showDialog({ code: 'warinning', message: '更多检查项暂未开放，敬请期待…', totastType: 'black', timeout: true });
    } else {
      wx.navigateTo({
        url: '../' + e.currentTarget.dataset.page + '/' + e.currentTarget.dataset.page,
      })
    }

  },

  onLoad(options) {
    console.log(options);
    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
    var that = this;
     if(JSON.stringify(options)!="{}"){

      const scene = decodeURIComponent(options.scene)
      wx.setStorage({
        key:"scene",
        data:scene
      }
      )
   
     }
    this.toastDialog = this.selectComponent("#toastDialog");//此处id和页面的 wxml中的id一致
    wx.setNavigationBarTitle({
      title: '首页',
    })
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    this.queryLayoutList()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage (res) {
    return {
      title: '迪佳极视',
      path: 'pages/index/index',
      // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete:function(res){
        // 不管成功失败都会执行
      }
    }
  },
  queryLayoutList: function () {
    var that = this
    request.postData('xcx/layoutList', {}, function (res) {
      // success
      console.log(res)
      that.setData({
        banerList: res.banerList,
        layoutList: res.layoutList
      })

    }, function (res) {
    });
  },
  gotoLink: function (e) {
    console.log(e.currentTarget.dataset)
    var that = this;

    if(e.currentTarget.dataset.status=='0'){
      return 
    }else if(e.currentTarget.dataset.status=='1' || e.currentTarget.dataset.status==undefined){
      that.setData({
        link: e.currentTarget.dataset.item.link,
        title: e.currentTarget.dataset.item.title,
      })
      wx.navigateTo({
        url: '../ys/ys?link=' + that.data.link + '&title=' + that.data.title,
      })
    }
   
  },
  onShow() {
    var that = this;
    //第一步判断用户登录状态
    let code = wx.getStorageSync("openid");
    if (code == undefined || code == "") { //如果token没有值，提示用户需要登录
      wx.redirectTo({
        url: '../login/login',
      })
    }else{
      var e = wx.getStorageSync("scene")
      if(that.data.type==0){
        request.postData('xcx/bindTest',e, function (res) {
          // success
          console.log(res)
          that.setData({
            type:1
          })
    
        }, function (res) {
        });
      }
      
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

})
